package org.rowan.infovis.soccer;

import com.sun.opengl.util.GLUT;
import javax.media.opengl.GL;

public class PlayerCategoryNav
{
    GLUT glut = new GLUT();
    private float HEIGHT = 303f;
    private float WIDTH = 115f;
    private float PAD = 3f;

    private float LINK_WIDTH = 110f;
    private float LINK_HEIGHT = 40f;

    private float fluency_X = PAD;
    private float fluency_Y = HEIGHT - LINK_HEIGHT - PAD;

    private boolean onfluency = true;
    boolean fluencySelected = true;

    private float coordination_X = PAD;
    private float coordination_Y = fluency_Y - LINK_HEIGHT - PAD;

    private boolean oncoordination = false;
    boolean coordinationSelected = false;

    private float easiness_X = PAD;
    private float easiness_Y = coordination_Y - LINK_HEIGHT - PAD;

    private boolean oneasiness = false;
    boolean easinessSelected = false;

    private float rapidity_X = PAD;
    private float rapidity_Y = easiness_Y - LINK_HEIGHT - PAD;
    private boolean onrapidity = false;
    boolean rapiditySelected = false;

    private float foot_X = PAD;
    private float foot_Y = rapidity_Y - LINK_HEIGHT - PAD;

    private boolean onfoot = false;
    boolean footSelected = false;

    private float torso_X = PAD;
    private float torso_Y = foot_Y - LINK_HEIGHT - PAD;

    private boolean ontorso = false;
    boolean torsoSelected = false;

    private float movement_X = PAD;
    private float movement_Y = torso_Y - LINK_HEIGHT - PAD;

    private boolean onmovement = false;
    boolean movementSelected = false;



    private float startX;
    private float startY;

    public float getHeight() {
        return HEIGHT;
    }

    public float getWidth() {
        return WIDTH;
    }

    public void draw(GL gl, float x, float y) {

        startX = x;
        startY = y;

        drawBorder(gl);

        drawFluency(gl);

        drawCoordination(gl);

        drawEasiness(gl);

        drawRapidity(gl);

        drawFoot(gl);

        drawTorso(gl);

        drawMovement(gl);
    }

    private void drawBorder(GL gl) {
        gl.glPushMatrix();
        gl.glColor3f(1f, 1f, 1f);
        gl.glBegin(GL.GL_QUADS);
        gl.glVertex2f(0f, 0f);
        gl.glVertex2f(0f, HEIGHT);
        gl.glVertex2f(WIDTH, HEIGHT);
        gl.glVertex2f(WIDTH, 0f);
        gl.glEnd();
        gl.glPopMatrix();
    }

    private void setLinkColor(GL gl) {
        gl.glColor3f(.8f, .8f, .8f);
    }

    private void setHoverLinkColor(GL gl) {
        gl.glColor3f(1f, 0f, 0f);
    }

    private void setSelectedLinkColor(GL gl) {
        gl.glColor3f(0f, 0, 1f);
    }

    private void drawFluency(GL gl) {
        if (fluencySelected) {
            setSelectedLinkColor(gl);
        } else if (onfluency) {
            setHoverLinkColor(gl);
        } else {
            setLinkColor(gl);
        }

        gl.glPushMatrix();
        gl.glBegin(GL.GL_QUADS);
        gl.glVertex2f(fluency_X, fluency_Y);
        gl.glVertex2f(fluency_X, fluency_Y + LINK_HEIGHT);
        gl.glVertex2f(fluency_X + LINK_WIDTH, fluency_Y + LINK_HEIGHT);
        gl.glVertex2f(fluency_X + LINK_WIDTH, fluency_Y);
        gl.glEnd();
        gl.glPopMatrix();

        gl.glPushMatrix();
        gl.glColor3f(0f, 0f, 0f);
        gl.glRasterPos2f(fluency_X + 25f, fluency_Y + 15f);
        glut.glutBitmapString(GLUT.BITMAP_9_BY_15, "Fluency");
        gl.glPopMatrix();
    }

    private void drawCoordination(GL gl) {
        if (coordinationSelected) {
            setSelectedLinkColor(gl);
        } else if (oncoordination) {
            setHoverLinkColor(gl);
        } else {
            setLinkColor(gl);
        }

        gl.glPushMatrix();
        gl.glBegin(GL.GL_QUADS);
        gl.glVertex2f(coordination_X, coordination_Y);
        gl.glVertex2f(coordination_X, coordination_Y + LINK_HEIGHT);
        gl.glVertex2f(coordination_X + LINK_WIDTH, coordination_Y + LINK_HEIGHT);
        gl.glVertex2f(coordination_X + LINK_WIDTH, coordination_Y);
        gl.glEnd();
        gl.glPopMatrix();

        gl.glPushMatrix();
        gl.glColor3f(0f, 0f, 0f);
        gl.glRasterPos2f(coordination_X + 2f, coordination_Y + 15f);
        glut.glutBitmapString(GLUT.BITMAP_9_BY_15, "Coordination");
        gl.glPopMatrix();
    }

    private void drawEasiness(GL gl) {
        if (easinessSelected) {
            setSelectedLinkColor(gl);
        } else if (oneasiness) {
            setHoverLinkColor(gl);
        } else {
            setLinkColor(gl);
        }

        gl.glPushMatrix();
        gl.glBegin(GL.GL_QUADS);
        gl.glVertex2f(easiness_X, easiness_Y);
        gl.glVertex2f(easiness_X, easiness_Y + LINK_HEIGHT);
        gl.glVertex2f(easiness_X + LINK_WIDTH, easiness_Y + LINK_HEIGHT);
        gl.glVertex2f(easiness_X + LINK_WIDTH, easiness_Y);
        gl.glEnd();
        gl.glPopMatrix();

        gl.glPushMatrix();
        gl.glColor3f(0f, 0f, 0f);
        gl.glRasterPos2f(easiness_X + 20f, easiness_Y + 15f);
        glut.glutBitmapString(GLUT.BITMAP_9_BY_15, "Easiness");
        gl.glPopMatrix();
    }

    private void drawRapidity(GL gl) {
        if (rapiditySelected) {
            setSelectedLinkColor(gl);
        } else if (onrapidity) {
            setHoverLinkColor(gl);
        } else {
            setLinkColor(gl);
        }

        gl.glPushMatrix();
        gl.glBegin(GL.GL_QUADS);
        gl.glVertex2f(rapidity_X, rapidity_Y);
        gl.glVertex2f(rapidity_X, rapidity_Y + LINK_HEIGHT);
        gl.glVertex2f(rapidity_X + LINK_WIDTH, rapidity_Y + LINK_HEIGHT);
        gl.glVertex2f(rapidity_X + LINK_WIDTH, rapidity_Y);
        gl.glEnd();
        gl.glPopMatrix();

        gl.glPushMatrix();
        gl.glColor3f(0f, 0f, 0f);
        gl.glRasterPos2f(rapidity_X + 21f, rapidity_Y + 15f);
        glut.glutBitmapString(GLUT.BITMAP_9_BY_15, "Rapidity");
        gl.glPopMatrix();
    }

    private void drawFoot(GL gl) {
        if (footSelected) {
            setSelectedLinkColor(gl);
        } else if (onfoot) {
            setHoverLinkColor(gl);
        } else {
            setLinkColor(gl);
        }

        gl.glPushMatrix();
        gl.glBegin(GL.GL_QUADS);
        gl.glVertex2f(foot_X, foot_Y);
        gl.glVertex2f(foot_X, foot_Y + LINK_HEIGHT);
        gl.glVertex2f(foot_X + LINK_WIDTH, foot_Y + LINK_HEIGHT);
        gl.glVertex2f(foot_X + LINK_WIDTH, foot_Y);
        gl.glEnd();
        gl.glPopMatrix();

        gl.glPushMatrix();
        gl.glColor3f(0f, 0f, 0f);
        gl.glRasterPos2f(foot_X + 35f, foot_Y + 15f);
        glut.glutBitmapString(GLUT.BITMAP_9_BY_15, "Foot");
        gl.glPopMatrix();
    }

    private void drawTorso(GL gl) {
        if (torsoSelected) {
            setSelectedLinkColor(gl);
        } else if (ontorso) {
            setHoverLinkColor(gl);
        } else {
            setLinkColor(gl);
        }

        gl.glPushMatrix();
        gl.glBegin(GL.GL_QUADS);
        gl.glVertex2f(torso_X, torso_Y);
        gl.glVertex2f(torso_X, torso_Y + LINK_HEIGHT);
        gl.glVertex2f(torso_X + LINK_WIDTH, torso_Y + LINK_HEIGHT);
        gl.glVertex2f(torso_X + LINK_WIDTH, torso_Y);
        gl.glEnd();
        gl.glPopMatrix();

        gl.glPushMatrix();
        gl.glColor3f(0f, 0f, 0f);
        gl.glRasterPos2f(torso_X + 30f, torso_Y + 15f);
        glut.glutBitmapString(GLUT.BITMAP_9_BY_15, "Torso");
        gl.glPopMatrix();
    }

    private void drawMovement(GL gl) {
        if (movementSelected) {
            setSelectedLinkColor(gl);
        } else if (onmovement) {
            setHoverLinkColor(gl);
        } else {
            setLinkColor(gl);
        }

        gl.glPushMatrix();
        gl.glBegin(GL.GL_QUADS);
        gl.glVertex2f(movement_X, movement_Y);
        gl.glVertex2f(movement_X, movement_Y + LINK_HEIGHT);
        gl.glVertex2f(movement_X + LINK_WIDTH, movement_Y + LINK_HEIGHT);
        gl.glVertex2f(movement_X + LINK_WIDTH, movement_Y);
        gl.glEnd();
        gl.glPopMatrix();

        gl.glPushMatrix();
        gl.glColor3f(0f, 0f, 0f);
        gl.glRasterPos2f(movement_X + 20f, movement_Y + 15f);
        glut.glutBitmapString(GLUT.BITMAP_9_BY_15, "Movement");
        gl.glPopMatrix();
    }


    private boolean insideFluency(int x, int y) {
        if (x >= fluency_X && x <= (fluency_X + LINK_WIDTH) &&
                y >= fluency_Y && y <= (fluency_Y + LINK_HEIGHT)) {
            return true;
        }

        return false;
    }

    private boolean insideCoordination(int x, int y) {
        if (x >= coordination_X && x <= (coordination_X + LINK_WIDTH) &&
                y >= coordination_Y && y <= (coordination_Y + LINK_HEIGHT)) {
            return true;
        }

        return false;
    }

    private boolean insideEasiness(int x, int y) {
        if (x >= easiness_X && x <= (easiness_X + LINK_WIDTH) &&
                y >= easiness_Y && y <= (easiness_Y + LINK_HEIGHT)) {
            return true;
        }

        return false;
    }

    private boolean insideRapidity(int x, int y) {
        if (x >= rapidity_X && x <= (rapidity_X + LINK_WIDTH) &&
                y >= rapidity_Y && y <= (rapidity_Y + LINK_HEIGHT)) {
            return true;
        }

        return false;
    }

    private boolean insideFoot(int x, int y) {
        if (x >= foot_X && x <= (foot_X + LINK_WIDTH) &&
                y >= foot_Y && y <= (foot_Y + LINK_HEIGHT)) {
            return true;
        }

        return false;
    }

    private boolean insideTorso(int x, int y) {
        if (x >= torso_X && x <= (torso_X + LINK_WIDTH) &&
                y >= torso_Y && y <= (torso_Y + LINK_HEIGHT)) {
            return true;
        }

        return false;
    }

    private boolean insideMovement(int x, int y) {
        if (x >= movement_X && x <= (movement_X + LINK_WIDTH) &&
                y >= movement_Y && y <= (movement_Y + LINK_HEIGHT)) {
            return true;
        }

        return false;
    }



    public void mouseMoved(int x, int y) {
        onfluency = false;
        oncoordination = false;
        oneasiness = false;
        onrapidity = false;
        onfoot = false;
        ontorso = false;
        onmovement = false;

        if (x >= startX &&
                x <= (startX + WIDTH) &&
                y >= startY &&
                y <= (startY + HEIGHT)) {
            int mouseX = x - (int) startX;
            int mouseY = y - (int) startY;

            if (insideFluency(mouseX, mouseY)) {
                onfluency = true;
            } else if (insideCoordination(mouseX, mouseY)) {
                oncoordination = true;
            } else if (insideEasiness(mouseX, mouseY)) {
                oneasiness = true;
            } else if (insideRapidity(mouseX, mouseY)) {
                onrapidity = true;
            } else if (insideFoot(mouseX, mouseY)) {
                onfoot = true;
            } else if (insideTorso(mouseX, mouseY)) {
                ontorso = true;
            } else if (insideMovement(mouseX, mouseY)) {
                onmovement = true;
            }
        }
    }

    public void mouseReleased(int x, int y) {

        if (x >= startX &&
                x <= (startX + WIDTH) &&
                y >= startY &&
                y <= (startY + HEIGHT)) {
            fluencySelected = false;
            coordinationSelected = false;
            easinessSelected = false;
            rapiditySelected = false;
            footSelected = false;
            torsoSelected = false;
            movementSelected = false;


            int mouseX = x - (int) startX;
            int mouseY = y - (int) startY;

            if (insideFluency(mouseX, mouseY)) {
                fluencySelected = true;
            } else if (insideCoordination(mouseX, mouseY)) {
                coordinationSelected = true;
            } else if (insideEasiness(mouseX, mouseY)) {
                easinessSelected = true;
            } else if (insideRapidity(mouseX, mouseY)) {
                rapiditySelected = true;
            } else if (insideFoot(mouseX, mouseY)) {
                footSelected = true;
            } else if (insideTorso(mouseX, mouseY)) {
                torsoSelected = true;
            } else if (insideMovement(mouseX, mouseY)) {
                movementSelected = true;
            }
        }
    }

    public int getMode() {
        if (fluencySelected) {
            return 1;
        }
        if (coordinationSelected) {
            return 2;
        }
        if (easinessSelected) {
            return 3;
        }
        if (rapiditySelected) {
            return 4;
        }
        if (footSelected) {
            return 5;
        }
        if (torsoSelected) {
            return 6;
        }
        if (movementSelected) {
            return 7;
        }

        return 0;
    }
}